package com.example.flex.controller;

import com.example.flex.domain.MenuEntity;
import com.example.flex.service.MenuService;
import com.example.flex.utils.common.PageEntity;
import com.example.flex.utils.response.BaseResponse;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 菜单表控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "菜单表接口")
@RequestMapping("menu")
public class MenuController{

    @Autowired
    private MenuService menuService;

    /**
     * 保存
     *
     * @param menuEntity 菜单表
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存菜单表")
    public BaseResponse<?> save(@RequestBody @ApiParam("菜单表") MenuEntity menuEntity){
        boolean saveFlag = menuService.save(menuEntity);
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询菜单表")
    public BaseResponse<?> info(@PathVariable @ApiParam("菜单表主键") Integer id){
        return BaseResponse.success(menuService.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询菜单表")
    public BaseResponse<?> list(@RequestBody @ApiParam("菜单表分页参数") PageEntity<MenuEntity> menuEntityPage){
        MenuEntity menuEntity = menuEntityPage.getData();
        QueryWrapper queryWrapper = getQueryWrapper(menuEntity);
        return BaseResponse.success(menuService.page(new Page<>(menuEntityPage.getPageNum(),menuEntityPage.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除菜单表")
    public BaseResponse<?> delete(@PathVariable @ApiParam("菜单表主键(列表)") Integer[] ids){
        return BaseResponse.success(menuService.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param menuEntity 菜单表
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改菜单表")
    public BaseResponse<?> edit(@RequestBody @ApiParam("菜单表") MenuEntity menuEntity){
        return BaseResponse.success(menuService.updateById(menuEntity),"修改成功");
    }

    /**
     * 获取queryWrapper
     *
     * @param menuEntity 菜单表
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(MenuEntity menuEntity){
        return QueryWrapper.create()
                    .eq(MenuEntity::getId,menuEntity.getId(),menuEntity.getId()!=null)
                    .eq(MenuEntity::getMenuName,menuEntity.getMenuName(),menuEntity.getMenuName()!=null)
                    .eq(MenuEntity::getPath,menuEntity.getPath(),menuEntity.getPath()!=null)
                    .eq(MenuEntity::getComponent,menuEntity.getComponent(),menuEntity.getComponent()!=null)
                    .eq(MenuEntity::getVisible,menuEntity.getVisible(),menuEntity.getVisible()!=null)
                    .eq(MenuEntity::getSorter,menuEntity.getSorter(),menuEntity.getSorter()!=null)
                    .eq(MenuEntity::getStatus,menuEntity.getStatus(),menuEntity.getStatus()!=null)
                    .eq(MenuEntity::getPerms,menuEntity.getPerms(),menuEntity.getPerms()!=null)
                    .eq(MenuEntity::getIcon,menuEntity.getIcon(),menuEntity.getIcon()!=null)
                    .eq(MenuEntity::getCreateBy,menuEntity.getCreateBy(),menuEntity.getCreateBy()!=null)
                    .eq(MenuEntity::getCreateTime,menuEntity.getCreateTime(),menuEntity.getCreateTime()!=null)
                    .eq(MenuEntity::getUpdateBy,menuEntity.getUpdateBy(),menuEntity.getUpdateBy()!=null)
                    .eq(MenuEntity::getUpdateTime,menuEntity.getUpdateTime(),menuEntity.getUpdateTime()!=null)
                    .eq(MenuEntity::getType,menuEntity.getType(),menuEntity.getType()!=null)
                    .eq(MenuEntity::getDelFlag,menuEntity.getDelFlag(),menuEntity.getDelFlag()!=null)
                    .eq(MenuEntity::getRemark,menuEntity.getRemark(),menuEntity.getRemark()!=null)
                    .eq(MenuEntity::getParentId,menuEntity.getParentId(),menuEntity.getParentId()!=null);
    }
}